package operator;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class UnionDemo1 {

    public static void main(String[] args) throws Exception{
        // 创建Stream
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        // 设置并行度为1 source 和 sink的并行度一致 不会发生 shuffle
        env.setParallelism(1);
        // 使用socket创建DataStream
        DataStreamSource<String> lines1 = env.socketTextStream("localhost", 8888);
        DataStreamSource<String> lines2 = env.socketTextStream("localhost", 9999);

        // 将两个流合并 需要数据类型一致
        DataStream<String> union = lines1.union(lines2);

        // sink
        union.print();

        // 执行flink 程序
        env.execute("union");

    }
}
